package com.enjoyor.soa.traffic.server.tsms.handler;

import com.enjoyor.soa.traffic.core.tsms.domain.MsgInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskExecuteRecord;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.server.tsms.dubbo.impl.DubboSchemeActionService;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.GeoToolsHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

import java.util.*;

/**
 * 接收数据，数据清理
 */
public class AlarmDistanceHandler extends BasicMsgHandler<MsgInfo> {
    private AlarmDistanceHandler() {
        super(1000, "车队位置过近报警");
    }

    private static AlarmDistanceHandler single;

    public static AlarmDistanceHandler getInstance() {
        if (single == null) {
            synchronized (AlarmDistanceHandler.class) {
                if (single == null) {
                    single = new AlarmDistanceHandler();
                    //最少5个线程 最多加到5个线程，积压20个累加
                    single.setMultiProcess(5, 20, 20);
                }
            }
        }
        return single;
    }
    @Override
    protected boolean task(int taskTimes) {
        return true;
    }

    GeoToolsHelper geoToolsHelper = new GeoToolsHelper();

    @SuppressWarnings("static-access")
	@Override
    protected boolean process(MsgInfo msg) {
        for (String key : RehearseGlobal.ActionDataPool.keySet()) {
            if (!key.equals(msg.getExecuteTaskId())) {
                if (RehearseGlobal.ActionDataPool.get(key).get("x") != null && RehearseGlobal.ActionDataPool.get(key).get("y") != null 
                		&& !(boolean)RehearseGlobal.ActionDataPool.get(key).get("stop")) {
                	double x = Double.parseDouble(RehearseGlobal.ActionDataPool.get(key).get("x").toString());
                	double y = Double.parseDouble(RehearseGlobal.ActionDataPool.get(key).get("y").toString());
                    if (geoToolsHelper.GetPointDistance(msg.getLongitude(), msg.getLatitude(), x, y) < DubboSchemeActionService.clashDistance) { // 200米间距
                    	Map<String, String> alarmDistancePool = RehearseGlobal.alarmDistancePool;
                    	boolean flag = false; 
                    	for (Map.Entry<String, String> m : alarmDistancePool.entrySet()) {
                    		String clashIds = m.getValue();
                    		if(clashIds.indexOf(msg.getExecuteTaskId()) >= 0 && clashIds.indexOf(key) >= 0) {
                    			long newDateL = new Date().getTime();
                    			long oldDateL = Long.parseLong(m.getKey());
                    			if(newDateL - oldDateL <= 60*1000) {
                        			flag = true;
                        			break;
                    			}else {
                    				alarmDistancePool.remove(m.getKey());
                    			}
                    		}
                    	}
                    	if(flag) {
                    		break;
                    	}
                    	String clashKey = "" + new Date().getTime();
                    	String clashIds = msg.getExecuteTaskId() + "," + key;
                    	alarmDistancePool.put(clashKey, clashIds);
                    	
                        com.alibaba.fastjson.JSONObject jsonObject4 = new com.alibaba.fastjson.JSONObject();
                        Map<String, String> distancePoint = new HashMap<String, String>();
                        String taskNameMsg = RehearseGlobal.ActionDataPool.get(msg.getExecuteTaskId()).get("taskName").toString();
                        String taskNameKey = RehearseGlobal.ActionDataPool.get(key).get("taskName").toString();
                        String content = taskNameMsg + "与" + taskNameKey + ",车队间距小于" + DubboSchemeActionService.clashDistance + "米！";
                        distancePoint.put("type", "warn");
                        distancePoint.put("recordTime", TimeHelper.dateToLongString());
                        distancePoint.put("content", content);
                        distancePoint.put("clash", content);
                        jsonObject4.put(RehearseGlobal.ActionDataPool.get(msg.getExecuteTaskId()).get("routeId").toString(), distancePoint);
                        InitializeHandler.getInstance().getServerPushServiceImpl().pushMessage("" + msg.getExecuteTaskId(), jsonObject4);
                        //记录信号机冲突信息
        				TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
        				taskExecuteRecord.setExecuteTaskId(msg.getExecuteTaskId());
        				taskExecuteRecord.setIsWarn("1");
        				taskExecuteRecord.setRecordTypeId(EnumDicPublic.WRAN_DISTANCE_CLASH.getKey());
        				taskExecuteRecord.setRecordTypeName(EnumDicPublic.WRAN_DISTANCE_CLASH.getValue());
        				taskExecuteRecord.setRecordContent(content);
        				InitializeHandler.getInstance().getTaskExecuteRecordService().insertRecord(taskExecuteRecord, "距离冲突");
                    }
                }
            }
        }

        return false;
    }


}
